﻿////Copyright 2011 - Jonathan Allen

//using System;
//using System.Linq;
//using System.Windows;
//using System.Windows.Interactivity;
//using Granite.Xaml;

//namespace Granite.Xaml.Behaviors
//{

//    /// <summary>
//    /// This behavior scrolls the associated control into view when it gets the focus.
//    /// </summary>
//	public sealed class ScrollIntoViewBehavior : Behavior<FrameworkElement>
//    {
//		/// <summary>
//		/// Determines if this behavior is active
//		/// </summary>
//        public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(ScrollIntoViewBehavior), new PropertyMetadata(false, OnIsEnabledChanged));

//		/// <summary>
//		/// Determines if this behavior is active
//		/// </summary>
//		public static bool GetIsEnabled(FrameworkElement control)
//        {
//            if (control == null)
//                throw new ArgumentNullException("control", "control is null.");

//            return (bool)control.GetValue(IsEnabledProperty);
//        }

//		/// <summary>
//		/// Determines if this behavior is active
//		/// </summary>
//		public static void SetIsEnabled(FrameworkElement control, bool value)
//        {
//            if (control == null)
//                throw new ArgumentNullException("control", "control is null.");

//            control.SetValue(IsEnabledProperty, value);
//        }

//		/// <summary>
//		/// 
//		///             Called after the behavior is attached to an AssociatedObject.
//		///             
//		/// </summary>
//		/// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
//        protected override void OnAttached()
//        {
//            AssociatedObject.GotFocus += AssociatedObject_GotFocus;
//        }

//		/// <summary>
//		/// 
//		///             Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
//		///             
//		/// </summary>
//		/// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks>
//        protected override void OnDetaching()
//        {
//            AssociatedObject.GotFocus -= AssociatedObject_GotFocus;
//        }

//        void AssociatedObject_GotFocus(object sender, RoutedEventArgs e)
//        {
//            AssociatedObject.BringIntoView();
//        }

//        static void OnIsEnabledChanged(DependencyObject control, DependencyPropertyChangedEventArgs e)
//        {
//            var newValue = (bool)e.NewValue;
//            var behaviors = Interaction.GetBehaviors(control);
//            var existingBehavior = behaviors.OfType<ScrollIntoViewBehavior>().FirstOrDefault();

//            if (!newValue)
//            {
//                if (existingBehavior != null)
//                    behaviors.Remove(existingBehavior);
//            }
//            else
//                behaviors.Add(new ScrollIntoViewBehavior());
//        }

//    }

//}

